/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
/*
 * Created on 26/05/2007 14:00:05
 */
package br.com.jteam.jfcm.model;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * <p>
 * Classe respons&aacute;vel por manter e gerenciar o hist&oacute;rico de arquivos.
 * 
 * @author Rafael Naufal <rafael.naufal@gmail.com>
 * @since 1.0
 * @version $Id$
 */
public class FileHistory implements Iterable<File>
{
	/**
	 * <p>
	 * Nome do arquivo de propriedade que cont&eacute;m as propriedades do hist&oacute;rico de
	 * arquivos, como por exemplo, seu tamanho.
	 */
	private static final String HISTORY_FILENAME = "history.properties";

	/**
	 * <p>
	 * Carregamento das propriedades do hist&oacute;rico.
	 */
	private static Properties historyProperties = new Properties();
	static
	{
		try
		{
			historyProperties.load(FileHistory.class.getClassLoader()
													.getResourceAsStream(HISTORY_FILENAME));
		} catch (Exception ioe)
		{
			throw new RuntimeException("Can't load configuration file "
							+ HISTORY_FILENAME + " in the CLASSPATH");
		}
	}

	/**
	 * <p>
	 * Vari&aacute;vel de Classe que mant&eacute;m as propriedades do hist&oacute;rico.
	 */
	private static int FILE_HISTORY_SIZE;

	/**
	 * <p>
	 * Inst&acirc;ncia &uacute;nica do hist&oacute;rico de arquivos. Deve haver apenas uma inst&acirc;ncia
	 * dessa classe durante toda a vida do sistema.
	 */
	private static FileHistory instance = new FileHistory();

	/**
	 * <p>
	 * Fila respons&aacute;vel por manter os &uacute;ltimos arquivos do
	 * hist&oacute;rico.
	 */
	private Queue<File> files;

	/**
	 * <p>
	 * Construtor padr&atilde;o. Inst&acirc;ncia dessa classe devem ser constru&iacute;das a partir
	 * do factory method {@link #getInstance()}
	 */
	private FileHistory()
	{
		files = new Queue<File>();
		FILE_HISTORY_SIZE =
						Integer.valueOf(((String) historyProperties.get("file_history_max_size")));
	}

	/**
	 * @return A inst&acirc;ncia do hist&oacute;rico de arquivos.
	 */
	public static FileHistory getInstance()
	{
		return instance;
	}

	/**
	 * <p>
	 * Adiciona um arquivo no hist&oacute;rico de arquivos.
	 * 
	 * @param file
	 *            arquivo a ser adicionado no hist&oacute;rico. Caso o arquivo seja
	 *            nulo, nada &eacute; feito.
	 */
	public void add(File file)
	{
		if (file == null)
		{
			return;
		}
		if (files.size() == FILE_HISTORY_SIZE)
		{
			files.remove();
		}
		if (files.contains(file))
		{
			return;
		}
		files.add(file);
	}

	/**
	 * <p>
	 * Produz um <code>iterator</code> da lista de arquivos do hist&oacute;rico
	 * corrente.
	 * 
	 * @return iterator da lista criado.
	 */
	public Iterator<File> iterator()
	{
		List<File> unmodifiableFiles = files.unmodifiableQueue();
		return unmodifiableFiles.iterator();
	}
}
